<?php
include 'config.inc.php';

function connect_to_db()
{
	$database = mysql_connect("localhost", "youth", "camp");
	mysql_select_db($database_name, $database);
}

function disconnect_from_db()
{
	mysql_close($database);
}

function find_in_table($table, $field, $value)
{
	connect_to_db();

	$query = "select * from $table WHERE $field LIKE '$value'";
	$results = mysql_query($query, $database) or die("Data not found."); 

	$xml_output  = "<?xml version=\"1.0\"?>\n";
	$xml_output .= "<entries>\n";
	
	for($x = 0 ; $x < mysql_num_rows($results) ; $x++){
	$row = mysql_fetch_assoc($results);
	$xml_output .= "\t<entry>\n";
	$xml_output .= "\t\t<date>" . $row['date'] . "</date>\n";
		// Escaping illegal characters
		$row['text'] = str_replace("&", "&", $row['text']);
		$row['text'] = str_replace("<", "<", $row['text']);
		$row['text'] = str_replace(">", "&gt;", $row['text']);
		$row['text'] = str_replace("\"", "&quot;", $row['text']);
	$xml_output .= "\t\t<text>" . $row['text'] . "</text>\n";
	$xml_output .= "\t</entry>\n";
	}
	
	$xml_output .= "</entries>";

	disconnect_from_db();
		
	return $xml_output; 	



}

function inject_to_table($table, $field, $value)
{
	connect_to_db();
	
	$query = "INSERT INTO $table ($field) VALUES($value)";
	mysql_query($query, $database);
}

function create_table($table)
{
	connect_to_db();
	
	$query = "create table $table (ID int NOT NULL AUTO_INCREMENT, PRIMARY KEY(ID))";
	mysql_query($query, $database);

	disconnect_from_db();
}


function create_field($table, $field, $field_type)
{
	connect_to_db();

	$query ="alter table $table add column $field $field_type";
	mysql_query($query, $database);

	disconnect_from_db();

}



?>
